package com.redis.base.multiple;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.redis.base.redis.IRedis;

public class RedisMultipleFactory  implements IRedis{
	
    private RedisConfig redisConfig;
   
    private IRedis redis;
	
	public RedisMultipleFactory() {
		
	}
	
	public RedisMultipleFactory(RedisConfig redisConfig) {
		this.redisConfig = redisConfig;
	}

	public void init() {
        if(StringUtils.isNotEmpty(redisConfig.getHostName()) && redisConfig.getHostName().split(",").length > 1) {
        	redis = new ClusterRedis(redisConfig);
        }else {
        	redis = new SingleRedis(redisConfig);
        }
	}

	@Override
	public void put(String key, String value) {
		redis.put(key, value);
		
	}

	@Override
	public void put(String key, String value, Long expire) {
		redis.put(key, value, expire);
		
	}

	@Override
	public void put(String key, Object value) {
			redis.put(key, value);
		
	}

	@Override
	public void put(String key, Object value, Long expire) {
		redis.put(key, value, expire);
		
	}

	@Override
	public String get(String key) {
		
		return redis.get(key);
	}

	@Override
	public void unLock(String... keys) {
		redis.unLock(keys);
		
	}

	@Override
	public Object deleteLike(String... keys) {
		return redis.deleteLike(keys);
	}

	@Override
	public boolean delete(String key) {
		return redis.delete(key);
	}

	@Override
	public <T> T get(String key, Class<T> objClass) {

		return redis.get(key, objClass);
	}

	@Override
	public <T> List<T> getList(String key, Class<T> objClass) {
		
		return redis.getList(key, objClass);
	}

	@Override
	public boolean tryLock(String lockKey) {
		return redis.tryLock(lockKey);
	}

	@Override
	public boolean tryLock(String lockKey, boolean isSpin) {
		return redis.tryLock(lockKey, isSpin);
	}

	@Override
	public boolean tryLock(String lockKey, boolean isSpin, int spinNum) {
		return redis.tryLock(lockKey, isSpin, spinNum);
	}

	@Override
	public boolean lock(String lockKey, boolean isSpin, int timeUnit) {
		return redis.lock(lockKey, isSpin, timeUnit);
	}

	@Override
	public long incr(String key) {
		return redis.incr(key);
	}

	@Override
	public Set<String> keys(String key) {
		return redis.keys(key);
	}
	
}
